package gdconf

import (
	"server_cluster/common/logger"
)

// 队长技能等级配置表

type CaptainSkillLevelConfig struct {
	Id                int32    `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 队长技能等级id
	CaptainSkillGroup int32    `csv:"SkillGroup"`                                // 队长技能组
	Level             int32    `csv:"Level,omitempty"`                           // 等级
	CaptainSkill      int32    `csv:"CaptainSkill,omitempty"`                    // 队长技能
	CostItemList      ItemList `csv:"UpgradeCost,omitempty"`                     // 升到下一级所消耗道具
}

func (c *CaptainSkillLevelConfig) TableName() string {
	return "config_captain_skill_level"
}

func (g *GameDataConfig) saveCaptainSkillLevelConfig() {
	saveTableToDb[CaptainSkillLevelConfig](g.Db, readTable[CaptainSkillLevelConfig](g.CsvPathPrefix+"CaptainSkillGroup.csv"))
}

func (g *GameDataConfig) loadCaptainSkillLevelConfig() {
	g.GameDataMaps.CaptainSkillLevelConfigMap = make(map[int32]map[int32]*CaptainSkillLevelConfig)
	for _, captainSkillLevelConfig := range loadTableFromDb[CaptainSkillLevelConfig](g.Db) {
		_, exist := g.GameDataMaps.CaptainSkillLevelConfigMap[captainSkillLevelConfig.CaptainSkillGroup]
		if !exist {
			g.GameDataMaps.CaptainSkillLevelConfigMap[captainSkillLevelConfig.CaptainSkillGroup] = make(map[int32]*CaptainSkillLevelConfig)
		}
		g.GameDataMaps.CaptainSkillLevelConfigMap[captainSkillLevelConfig.CaptainSkillGroup][captainSkillLevelConfig.Level] = captainSkillLevelConfig
	}
	logger.Info("CaptainSkillLevelConfig Count: %v", len(g.GameDataMaps.CaptainSkillLevelConfigMap))
}

func GetCaptainSkillLevelConfigByGroupAndLevel(group int32, level int32) *CaptainSkillLevelConfig {
	value1, exist := CONF.GameDataMaps.CaptainSkillLevelConfigMap[group]
	if !exist {
		logger.Error("[ConfigNotFound] CaptainSkillLevelConfig, group: %v, level: %v", group, level)
		return nil
	}
	value2, exist := value1[level]
	if !exist {
		logger.Error("[ConfigNotFound] CaptainSkillLevelConfig, group: %v, level: %v", group, level)
		return nil
	}
	return value2
}

func GetCaptainSkillLevelConfigMap() map[int32]map[int32]*CaptainSkillLevelConfig {
	return CONF.GameDataMaps.CaptainSkillLevelConfigMap
}
